package com.lebang.schedule;

import com.lebang.component.RedisClient;
import com.lebang.component.RedisSdkClient;
import com.lebang.domain.RankMember;
import com.lebang.entity.member.LbbMember;
import com.lebang.service.RankingService;
import com.lebang.service.buss.MemberService;
import com.lebang.service.sys.DictService;
import com.lebang.service.sys.RankMemberService;
import com.lebangweb.common.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

import static com.lebang.common.validcode.ValidCodeUtil.ID_CARD_COUNT;
import static com.lebang.controller.api.RankingApi.RANKING_DAY;
import static com.lebang.controller.api.RankingApi.RANKING_WEEK;

/**
 * @Author: hwq
 * @Date: 2019/10/9 15:46
 * @Version 1.0
 */
@Slf4j
@Component
public class RankTask {
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private RankingService  rankingService;
    @Value("${info.is-cences}")
    private Boolean isStar;
    @Autowired
    private RedisSdkClient sdkClient;
    @Autowired
    private RankMemberService rankMemberService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private DictService dictService;

    /**
     *  每日日榜结算  每天23:59:59
     */
    @Scheduled(cron = "59 59 23 * * ?")
    public void execute() {
        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rankday", UUIDUtil.M(),300000)) {
            log.info("开始结算日榜...");
            rankingService.dayOver();
        }else {
            //其他系统执行
            log.info("日榜其他系统执行");
        }
        log.info("统计完成...");
    }

    /**
     *  每周天晚上23:59:59点结算周榜
     */
    @Scheduled(cron = "59 59 23 ? * SUN")
    public void execute2() {
        //重置每周检测上限
        redisClient.del(ID_CARD_COUNT);
        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rank:execute2", UUIDUtil.M(),300000)) {
            log.info("开始结算周榜...");
            rankingService.weekOver();
        }else {
            //其他系统执行
            log.info("周榜其他系统执行");
        }
        log.info("统计完成...");
    }
    /**
     *  每日检查排行榜待领奖的通知用户领取
     */
    @Scheduled(cron = "0 0 10 * * ?")
    public void execute21() {
        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rank:execute21", UUIDUtil.M(),300000)) {
            log.info("每日检查排行榜待领奖的通知用户领取...");
            rankingService.recordPush();
        }else {
            //其他系统执行
            log.info("其他系统执行");
        }
        log.info("统计完成...");
    }
    /**
     *  每日日榜检查排名后通知
     */
    @Scheduled(cron = "0 0 19 * * ?")
    public void execute22() {
        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rank:execute22", UUIDUtil.M(), 300000)) {
            log.info("开始每日日榜检查排名后通知...");
            rankingService.rankPush(RANKING_DAY);
        }else {
            //其他系统执行
            log.info("其他系统执行");
        }
        log.info("统计完成...");
    }
/*    *
     *  每日周榜检查排名后通知
     */
    @Scheduled(cron = "0 0 20 * * ?")
    public void execute32() {
        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rank:execute32", UUIDUtil.M(),300000)) {
            log.info("开始每日周榜检查排名后通知...");
            rankingService.rankPush(RANKING_WEEK);
        }else {
            //其他系统执行
            log.info("其他系统执行");
        }
        log.info("统计完成...");
    }

    /**
     * 每周一早上0点03执行一次改名
     */
    @Scheduled(cron = "0 3 0 ? * MON")
    public void  updateName(){



        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rank:updatename", UUIDUtil.M(),300000)){
            //所有虚假的账号
            List<RankMember> rankMemberList = rankMemberService.selectByExample(null);
            rankMemberList.forEach(s->{
                LbbMember lbbMember = memberService.selectMemberById(s.getMemberId());
                lbbMember.setNickname("尾号"+String.valueOf(RandomUtils.nextInt(1000,9999)));
                memberService.updateMemberById(lbbMember);
                s.setModifyTime(LocalDateTime.now());
                rankMemberService.updateById(s);
            });
        }else {
            //其他系统执行
            log.info("其他系统执行");
        }
        log.info("修改完成...");

    }

    /**
     * 每2小时跑一次，修改钱
     * @return
     */
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void updateScore(){

        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rank:updatescore", UUIDUtil.M(),300000)){

            int rankMaxDiff = dictService.selectIntByKeyName("rank_max_diff");
            //所有虚假的账号
            List<RankMember> rankMemberList = rankMemberService.selectByExample(null);
            int count  = rankingService.getMax(rankMemberList);

            for(RankMember s:rankMemberList){
                Double score = redisClient.zscore(RANKING_DAY,s.getMemberId());
                if(score!=null&&count>0){
                    if(Double.valueOf(score).intValue()-count>rankMaxDiff){
                        continue;
                    }
                }
                int random = RandomUtils.nextInt(s.getStar(),s.getEnd());
                random = (random/50)*50==0?50:(random/50)*50;
                rankingService.memberAddScore(s.getMemberId(), random);
            }


        }else {
            //其他系统执行
            log.info("其他系统执行");
        }
        log.info("修改完成...");
    }


    /**
     * 23:58，修改钱
     * @return
     */
    @Scheduled(cron = "0 58 23 * * ?")
    public void updateMoney(){

        if(!isStar){
            return;
        }
        if(redisClient.tryGetDistributedLock("lock:Scheduled:rank:updatemoney", UUIDUtil.M(),300000)){

            int rankMaxDiff = dictService.selectIntByKeyName("rank_max_diff");
            //所有虚假的账号
            List<RankMember> rankMemberList = rankMemberService.selectByExample(null);
            int count  = rankingService.getMax(rankMemberList);

            for(RankMember s:rankMemberList){
                Double score = redisClient.zscore(RANKING_DAY,s.getMemberId());
                if(score!=null&&count>0){
                    if(Double.valueOf(score).intValue()-count>rankMaxDiff){
                        continue;
                    }
                }
                int random = RandomUtils.nextInt(s.getStar(),s.getEnd());
                random = (random/50)*50==0?50:(random/50)*50;
                rankingService.memberAddScore(s.getMemberId(), random);
            }


        }else {
            //其他系统执行
            log.info("其他系统执行");
        }
        log.info("修改完成...");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            System.out.println(RandomUtils.nextInt(1000,9999));
        }

    }
}
